package co.edu.unal.satacredito.administration.view.establishment;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import javax.annotation.PostConstruct;
import javax.ejb.EJB;
import javax.faces.application.FacesMessage;
import javax.faces.bean.ManagedBean;
import javax.faces.context.FacesContext;
import javax.faces.model.SelectItem;

import org.bson.types.ObjectId;

import co.edu.unal.satacredito.administration.view.general.AdministrationBean;
import co.edu.unal.satacredito.database.entity.Brand;
import co.edu.unal.satacredito.infrastructure.dto.BrandDTO;
import co.edu.unal.satacredito.infrastructure.dto.EstablishmentDTO;
import co.edu.unal.satacredito.mediation.BrandEJB;
import co.edu.unal.satacredito.mediation.BrandEJBLocal;
import co.edu.unal.satacredito.mediation.EstablishmentEJBLocal;

@ManagedBean(name = "edit_establishments")
public class EditEstablishment extends AdministrationBean {

	@EJB
	EstablishmentEJBLocal establishmentEJB;
	@EJB
	BrandEJBLocal brandEJB;
	
	private String establishmentId;
	private EstablishmentDTO establishmentDTO;
	private String marca;
	private SelectItem[] marcas;

	@PostConstruct
	public void init() {
		if (establishmentDTO == null) {
			establishmentDTO = new EstablishmentDTO();
		}
	}
	

	public String loadCreate(){
		establishmentDTO.setId(null);
		return SUCCESS;
	}

	public String loadEdit() {
		Map<String, String> params = FacesContext.getCurrentInstance()
				.getExternalContext().getRequestParameterMap();
		
		establishmentId = params.get("establishmentid");
		establishmentDTO.setId(establishmentId);
		
		try {
			establishmentDTO = establishmentEJB.findEstablishment(establishmentId);
			return SUCCESS;
		} catch (Exception e) {
			FacesContext facesContext = FacesContext.getCurrentInstance();
			facesContext.addMessage(null, new FacesMessage(
					"Lo sentimos ocurrio un error inesperado"));
			return FAILED;
		}
	}
	
	public String doCreate(){
		try {
			BrandDTO marcaDTO = brandEJB.findBrand(marca);
			Brand marcaSelect = new Brand(marcaDTO.getId());
			marcaSelect.setBrandName(marcaDTO.getBrandName());
			marcaSelect.setNit(marcaDTO.getNit());
			marcaSelect.setCEOFirstName(marcaDTO.getCEOFirstName());
			marcaSelect.setCEOLastName(marcaDTO.getCEOLastName());
			marcaSelect.setCEODocument(marcaDTO.getCEODocument());
			marcaSelect.setEmail(marcaDTO.getEmail());
			marcaSelect.setPhoneNumber(marcaDTO.getPhoneNumber());
			marcaSelect.setStatus(marcaDTO.getStatus());
			
			establishmentDTO.setBrand(marcaSelect);
			
			establishmentEJB.create(establishmentDTO);
			
			FacesContext facesContext = FacesContext.getCurrentInstance();
			facesContext.addMessage(null, new FacesMessage(
					"Nuevo establecimiento creado"));
			
			return SUCCESS;
			
		} catch (Exception e) {
			FacesContext facesContext = FacesContext.getCurrentInstance();
			facesContext.addMessage(null, new FacesMessage(
					"Error al crear el establecimiento"));
			
			return FAILED;
		}
		
	}

	public String doEdit() {
		try {
			
			BrandDTO marcaDTO = brandEJB.findBrand(marca);
			Brand marcaSelect = new Brand(marcaDTO.getId());
			marcaSelect.setBrandName(marcaDTO.getBrandName());
			marcaSelect.setNit(marcaDTO.getNit());
			marcaSelect.setCEOFirstName(marcaDTO.getCEOFirstName());
			marcaSelect.setCEOLastName(marcaDTO.getCEOLastName());
			marcaSelect.setCEODocument(marcaDTO.getCEODocument());
			marcaSelect.setEmail(marcaDTO.getEmail());
			marcaSelect.setPhoneNumber(marcaDTO.getPhoneNumber());
			marcaSelect.setStatus(marcaDTO.getStatus());
			
			establishmentDTO.setBrand(marcaSelect);
			
			establishmentEJB.edit(establishmentDTO.getId(),establishmentDTO);
			
			FacesContext facesContext = FacesContext.getCurrentInstance();
			facesContext.addMessage(null, new FacesMessage(
					"Actualización finalizada"));
			return SUCCESS;
			
		} catch (Exception e) {
			FacesContext facesContext = FacesContext.getCurrentInstance();
			facesContext.addMessage(null, new FacesMessage(
					"El establecimiento no se pudo actualizar"));
			return FAILED;
		}
	}
	
	public String doDelete(){
		try {
			
			establishmentEJB.delete(establishmentDTO.getId());
			
			FacesContext facesContext = FacesContext.getCurrentInstance();
			facesContext.addMessage(null, new FacesMessage(
					"La marca a sido borrada"));
			return SUCCESS;
			
		} catch (Exception e) {
			FacesContext facesContext = FacesContext.getCurrentInstance();
			facesContext.addMessage(null, new FacesMessage(
					"No se pudo borrar la Marca"));
			
			return FAILED;
		}
	}
	
	public String cancel(){
		return SUCCESS;
	}
	
	public String getEstablishmentId() {
		return establishmentId;
	}

	public void setEstablishmentId(String establishmentId) {
		this.establishmentId = establishmentId;
	}

	public String getMarca() {
		return marca;
	}

	public void setMarca(String marca) {
		this.marca = marca;
	}

	public List<SelectItem> getMarcas() {
		List<SelectItem> selectItems = new ArrayList<SelectItem>();
		List<BrandDTO> marcasDTO = new ArrayList<BrandDTO>();
		
		marcasDTO = brandEJB.findAllBrands();

		for (BrandDTO estaDTO : marcasDTO) {
			selectItems.add(new SelectItem(estaDTO.getId().toString(), estaDTO.getBrandName()));
		}
		
		return selectItems;
	}

	public void setMarcas(SelectItem[] marcas) {
		this.marcas = marcas;
	}

	public EstablishmentDTO getEstablishmentDTO() {
		return establishmentDTO;
	}

	public void setEstablishmentDTO(EstablishmentDTO establishmentDTO) {
		this.establishmentDTO = establishmentDTO;
	}

}
